
import { Component, OnInit, Input, OnDestroy } from '@angular/core';
import { Subscription } from 'rxjs';
import { AppGlobalService } from '../../shared/service/app-global.service';
import { MessageService } from '../../shared/service/message.service';
import { HttpService } from '../../shared/service/http.service';

//tslint:disable
@Component({
  selector: 'app-store-list',
  templateUrl: './store-list.component.html',
  styleUrls: ['./store-list.component.scss']
})
export class StoreListComponent implements OnInit, OnDestroy {

  @Input() tagData: any = {};
  subscription: Subscription = null;
  params: any = {};
  pageNo = 1;
  pageSize: number;
  pageTotal: number;
  constructor(private appGlobalService: AppGlobalService, private message: MessageService, private http: HttpService) { }
  ngOnInit() {
    this.params = {
      content: {},
      id: this.tagData['id'],
      sessionId: localStorage.getItem('sessionId')
    };
    this.pageSize = this.tagData['pageSize'];
    this.pageTotal = this.tagData['pageTotal'];
    if (!this.subscription) {
      this.subscription = this.message.getMessage().subscribe(param => {
        this.pageNo = 1;
        this.params['content'] = param;
        this.params['content']['pageNo'] = this.pageNo;
        this.http.post('application/submit', this.params, res => {
          if (res['success']) {
            this.tagData['data'] = res['data']['list'];
          } else {
            this.appGlobalService.showToast(res['message'], 2000, 'middle');
          }
        });
      });
    }
  }

  ngOnDestroy(): void {
    this.subscription.unsubscribe();
  }

  clickLink(target, id) {
    const pageParams = {
      pageId: target,
      params: { id: id },
      sessionId: localStorage.getItem('sessionId')
    };
    this.appGlobalService.goToForward(pageParams);
  }

  doRefresh(event) { }

  loadData(event) {
    this.pageNo = this.pageNo + 1;
    this.params['content']['pageNo'] = this.pageNo;
    this.http.post('application/submit', this.params, res => {
      if (res['success']) {
        if (Array.isArray(res['data']['list']) && res['data']['list'].length > 0) {
          res['data']['list'].map(item => {
            this.tagData['data'].push(item);
          });
        } else {
        }
        event.target.complete();
      } else {
        this.pageNo = this.pageNo - 1;
        this.appGlobalService.showToast(res['message'], 2000, 'middle');
        event.target.complete();
      }
    });
  }
}
